﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace JGSY.CMS.LowCode.Platform.Migrations
{
    /// <inheritdoc />
    public partial class AddMonitoringTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "monitor_error_logs",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SessionId = table.Column<string>(type: "text", nullable: false),
                    UserId = table.Column<string>(type: "text", nullable: true),
                    ErrorType = table.Column<string>(type: "text", nullable: false),
                    ErrorMessage = table.Column<string>(type: "text", nullable: true),
                    ErrorStack = table.Column<string>(type: "text", nullable: true),
                    ErrorFilename = table.Column<string>(type: "text", nullable: true),
                    ErrorLineNumber = table.Column<int>(type: "integer", nullable: true),
                    ErrorColumnNumber = table.Column<int>(type: "integer", nullable: true),
                    PageUrl = table.Column<string>(type: "text", nullable: false),
                    UserAgent = table.Column<string>(type: "text", nullable: true),
                    Severity = table.Column<string>(type: "text", nullable: false),
                    Status = table.Column<string>(type: "text", nullable: false),
                    Fingerprint = table.Column<string>(type: "text", nullable: true),
                    Timestamp = table.Column<long>(type: "bigint", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_monitor_error_logs", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "monitor_performance_metrics",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SessionId = table.Column<string>(type: "text", nullable: false),
                    UserId = table.Column<string>(type: "text", nullable: true),
                    MetricName = table.Column<string>(type: "text", nullable: false),
                    MetricValue = table.Column<decimal>(type: "numeric(10,2)", precision: 10, scale: 2, nullable: false),
                    MetricRating = table.Column<string>(type: "text", nullable: true),
                    PageUrl = table.Column<string>(type: "text", nullable: false),
                    UserAgent = table.Column<string>(type: "text", nullable: true),
                    BrowserName = table.Column<string>(type: "text", nullable: true),
                    DeviceType = table.Column<string>(type: "text", nullable: true),
                    Timestamp = table.Column<long>(type: "bigint", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_monitor_performance_metrics", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "monitor_session_summary",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SessionId = table.Column<string>(type: "text", nullable: false),
                    UserId = table.Column<string>(type: "text", nullable: true),
                    SessionStart = table.Column<long>(type: "bigint", nullable: false),
                    SessionEnd = table.Column<long>(type: "bigint", nullable: true),
                    SessionDuration = table.Column<int>(type: "integer", nullable: true),
                    PageViews = table.Column<int>(type: "integer", nullable: false),
                    ErrorCount = table.Column<int>(type: "integer", nullable: false),
                    AvgLCP = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
                    AvgFID = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
                    AvgCLS = table.Column<decimal>(type: "numeric(6,4)", precision: 6, scale: 4, nullable: true),
                    AvgFCP = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
                    AvgTTFB = table.Column<decimal>(type: "numeric(8,2)", precision: 8, scale: 2, nullable: true),
                    BrowserName = table.Column<string>(type: "text", nullable: true),
                    BrowserVersion = table.Column<string>(type: "text", nullable: true),
                    OSName = table.Column<string>(type: "text", nullable: true),
                    OSVersion = table.Column<string>(type: "text", nullable: true),
                    DeviceType = table.Column<string>(type: "text", nullable: true),
                    Country = table.Column<string>(type: "text", nullable: true),
                    IPAddress = table.Column<string>(type: "text", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_monitor_session_summary", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "monitor_user_interactions",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    SessionId = table.Column<string>(type: "text", nullable: false),
                    InteractionType = table.Column<string>(type: "text", nullable: false),
                    PageUrl = table.Column<string>(type: "text", nullable: false),
                    TargetElement = table.Column<string>(type: "text", nullable: true),
                    ElementText = table.Column<string>(type: "text", nullable: true),
                    RouteFrom = table.Column<string>(type: "text", nullable: true),
                    RouteTo = table.Column<string>(type: "text", nullable: true),
                    ScrollDepth = table.Column<int>(type: "integer", nullable: true),
                    FormData = table.Column<string>(type: "text", nullable: true),
                    Duration = table.Column<int>(type: "integer", nullable: true),
                    Timestamp = table.Column<long>(type: "bigint", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_monitor_user_interactions", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_monitor_error_logs_CreatedAt",
                table: "monitor_error_logs",
                column: "CreatedAt");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_error_logs_ErrorType",
                table: "monitor_error_logs",
                column: "ErrorType");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_error_logs_Fingerprint",
                table: "monitor_error_logs",
                column: "Fingerprint");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_error_logs_SessionId",
                table: "monitor_error_logs",
                column: "SessionId");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_error_logs_Severity",
                table: "monitor_error_logs",
                column: "Severity");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_performance_metrics_MetricName",
                table: "monitor_performance_metrics",
                column: "MetricName");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_performance_metrics_MetricName_Timestamp",
                table: "monitor_performance_metrics",
                columns: new[] { "MetricName", "Timestamp" });

            migrationBuilder.CreateIndex(
                name: "IX_monitor_performance_metrics_SessionId",
                table: "monitor_performance_metrics",
                column: "SessionId");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_performance_metrics_Timestamp",
                table: "monitor_performance_metrics",
                column: "Timestamp");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_session_summary_SessionId",
                table: "monitor_session_summary",
                column: "SessionId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_monitor_session_summary_SessionStart",
                table: "monitor_session_summary",
                column: "SessionStart");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_session_summary_UserId",
                table: "monitor_session_summary",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_user_interactions_InteractionType",
                table: "monitor_user_interactions",
                column: "InteractionType");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_user_interactions_SessionId",
                table: "monitor_user_interactions",
                column: "SessionId");

            migrationBuilder.CreateIndex(
                name: "IX_monitor_user_interactions_Timestamp",
                table: "monitor_user_interactions",
                column: "Timestamp");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "monitor_error_logs");

            migrationBuilder.DropTable(
                name: "monitor_performance_metrics");

            migrationBuilder.DropTable(
                name: "monitor_session_summary");

            migrationBuilder.DropTable(
                name: "monitor_user_interactions");
        }
    }
}
